<?php
/**
 * User: fu
 * Date: 12-3-21
 * Time: 下午2:48
 */
class Generation
{
    protected $rep='@id';
    protected $publish;
    protected $docroot;
    public  $errorinfo;
    public function __construct(Publish $publish){
        $this->publish=$publish;
        $this->docroot =DOCROOT;
    }
    public function makefile($mid){
        if(!file_exists($this->docroot)){
            $this->errorinfo='根目录：'.$this->docroot.' 不存在';
            return false;
        }

        $manarr=$this->publish->querymid($mid);
        $page = $this->publish->getpage($mid,1);
        $attach=new Attach();
        for($i=1;$i<=$page->gettotalpage();$i++){
            $datar=$this->publish->show($mid,$i,'*','');
            $page->setcurpage($i);
            //生成文件、
            if($manarr[0]['name_rule']!==''&&$manarr[0]['file_tpl']!==''){
                foreach ($datar as $key=>$val) {
                    $filename = str_replace($this->rep,$val['id'],$manarr[0]['name_rule']);
                    $datar[$key]['url'] = SITEDIR.$filename;
                    if(!$this->makesingle($mid,$datar[$key],SITEDIR.$filename,SITEDIR.$manarr[0]['file_tpl'])){
                        return false;
                    }
                }

            }
            //生成文件列表
            if($manarr[0]['list_flie']!==''){
                if(!$this->makelist($mid,$datar,$page, SITEDIR.$manarr[0]['list_tpl'],SITEDIR.$manarr[0]['list_flie'],$i)){
                    return false;
                }
            }
            //生成附件
            foreach (explode(',', $manarr[0]['attach_id']) as $aid) {
                $attarr=$attach->queryid($aid);
                if(!$this->makeacth($mid,$datar,SITEDIR.$attarr[0]['tpl'],SITEDIR.$attarr[0]['file'])){
                    return false;
                }
            }


        }
        return true;

    }
    /**
     * 生成单个文件
     * @param $mid
     * @param $data
     * @param $filename
     * @param $file_tpl
     * @return bool
     */
    protected function makesingle($mid,$data,$filename,$file_tpl){
        if($file_tpl===SITEDIR){
            return true;
        }

        ob_start();
        $publish=new Publish();
        if($file_tpl!==''&&file_exists($this->docroot .$file_tpl)){
            include $this->docroot .$file_tpl;
        }
        $out = ob_get_contents();
        ob_end_clean();
        $handle=fopen($this->docroot .$filename,'w');
        fwrite($handle,$out);
        fclose($handle);
        return true;

    }
    protected function makelist($mid,$datar,Page $page,$list_tpl,$list_flie,$i){
        if($list_tpl===SITEDIR){
            return true;
        }
        ob_start();
        $publish=new Publish();
        if(!($list_tpl!==''&&is_file($this->docroot .$list_tpl))){
            return false;
        }
        include $this->docroot .$list_tpl;
        $out = ob_get_contents();
        ob_end_clean();
        if(!empty($page->linkarr)){
            //获取 link
            foreach ($page->linkarr as $link) {
                if(preg_match('/href=[\'|"](.*?)[\'|"].*?>'.$i.'</',$link,$matchs)){
                    break;
                }
            }

            $list_flie=$matchs[1];
        }


        if($list_flie!==''){
            $handle=fopen($this->docroot .$list_flie,'w');
            fwrite($handle,$out);
            fclose($handle);
        }
        return true;
    }
    /**
     * 生成附带发布
     * @param $mid
     * @param $datar
     * @param $list_tpl
     * @param $list_flie
     * @return bool
     */
    protected function makeacth($mid,$datar,$list_tpl,$list_flie){
        if($list_tpl===SITEDIR){
            return true;
        }

        if(!is_file($this->docroot .$list_tpl)){
            $this->errorinfo='生成附带发布时：'.$this->docroot.$list_tpl.' 文件不存在';
            return false;
        }

        ob_start();
        include $this->docroot .$list_tpl;
        $out = ob_get_contents();
        ob_end_clean();
        try{
            if($list_flie!==''){
                $handle=fopen($this->docroot .$list_flie,'w');
                fwrite($handle,$out);
                fclose($handle);
            }
            return true;
        }catch(Exception $e){
            $this->errorinfo='生成附带发布失败';
            return false;
        }

    }
}
